package com.roadjava.tutor.service.impl;

import com.roadjava.tutor.bean.dto.UserDTO;
import com.roadjava.tutor.bean.entity.Member;
import com.roadjava.tutor.bean.enums.MemberStatusEnum;
import com.roadjava.tutor.bean.enums.OrderStatusEnum;
import com.roadjava.tutor.bean.res.ResultDTO;
import com.roadjava.tutor.bean.vo.StudentDemandVO;
import com.roadjava.tutor.bean.vo.UserVO;
import com.roadjava.tutor.mapper.MemberMapper;
import com.roadjava.tutor.service.MemberService;
import com.roadjava.tutor.util.DateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zhaodaowen
 * @see <a href="http://www.roadjava.com">乐之者java</a>
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Resource
    private MemberMapper memberMapper;
    @Override
    public ResultDTO<String> register(UserDTO userDTO) {
        Member member = userDTO.toMember();
        int count = memberMapper.insert(member);
        if (count != 1) {
            return ResultDTO.buildFailure("添加失败");
        }else {
            return ResultDTO.buildSuccess("添加成功");
        }
    }

    @Override
    public ResultDTO<UserVO> login(UserDTO userDTO) {
        Member member = memberMapper.validateLogin(userDTO);
        if (member == null) {
            return ResultDTO.buildFailure("用户名或密码不正确");
        }else {
            if (member.getStatus().equals(MemberStatusEnum.DISABLED.getStatus())) {
                return ResultDTO.buildFailure("用户已被禁用");
            }
            // 更新登录时间和ip
            Member memberToUpdate = new Member();
            memberToUpdate.setId(member.getId());
            memberToUpdate.setLastLoginTime(DateUtil.date2Str(LocalDateTime.now()));
            memberToUpdate.setLastLoginIp(userDTO.getIp());
            memberMapper.update(memberToUpdate);
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(member,userVO);
            return ResultDTO.buildSuccess(userVO);
        }
    }

    @Override
    public ResultDTO<Member> selectOneById(Long id) {
        Member member = memberMapper.selectOneById(id);
        return member != null ? ResultDTO.buildSuccess(member) : ResultDTO.buildFailure("id不存在");
    }

    @Override
    public ResultDTO<String> trueUpdate(UserDTO userDTO) {
        Member member = userDTO.toMember();
        int result = memberMapper.update(member);
        if (result == 1) {
            return ResultDTO.buildSuccess("修改成功");
        }else {
            return ResultDTO.buildSuccess("修改失败");
        }
    }

    @Override
    public ResultDTO<List<Member>> loadMemberTable(UserDTO dto) {
        List<Member> list = memberMapper.listByPage(dto);
        if (CollectionUtils.isEmpty(list)) {
            return ResultDTO.buildSuccess(list, (long) list.size());
        }
        long count = memberMapper.selectCount(dto);
        return ResultDTO.buildSuccess(list,count);
    }
}
